package com.example.questionnaires.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnaires.common.Result;
import com.example.questionnaires.entity.News;
import com.example.questionnaires.service.NewsService;
import com.example.questionnaires.common.PageResult;
import com.example.questionnaires.dto.NewsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/news")
@Slf4j
public class NewsController {
    @Autowired
    private NewsService newsService;

    @GetMapping("/list")
    public Result<PageResult<NewsDTO>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("获取新闻列表, page: {}, size: {}", page, size);
        try {
            return newsService.list(page, size);
        } catch (Exception e) {
            log.error("获取新闻列表失败", e);
            return Result.error("获取新闻列表失败");
        }
    }

    @GetMapping("/{id}")
    public Result<NewsDTO> getById(@PathVariable Long id) {
        log.info("获取新闻详情, id: {}", id);
        try {
            return newsService.getById(id);
        } catch (Exception e) {
            log.error("获取新闻详情失败", e);
            return Result.error("获取新闻详情失败");
        }
    }

    @PostMapping
    public Result<Void> add(@RequestBody NewsDTO newsDTO) {
        log.info("添加新闻: {}", newsDTO);
        try {
            return newsService.add(newsDTO);
        } catch (Exception e) {
            log.error("添加新闻失败", e);
            return Result.error("添加新闻失败");
        }
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody NewsDTO newsDTO) {
        log.info("更新新闻, id: {}, news: {}", id, newsDTO);
        try {
            newsDTO.setId(id);
            return newsService.update(newsDTO);
        } catch (Exception e) {
            log.error("更新新闻失败", e);
            return Result.error("更新新闻失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除新闻, id: {}", id);
        try {
            return newsService.delete(id);
        } catch (Exception e) {
            log.error("删除新闻失败", e);
            return Result.error("删除新闻失败");
        }
    }

    @GetMapping("/latest")
    public Result<List<NewsDTO>> getLatest() {
        log.info("获取最新新闻");
        try {
            return newsService.getLatest();
        } catch (Exception e) {
            log.error("获取最新新闻失败", e);
            return Result.error("获取最新新闻失败");
        }
    }

    @PutMapping("/{id}/views")
    public Result<Void> incrementViews(@PathVariable Long id) {
        log.info("增加新闻浏览量, id: {}", id);
        try {
            return newsService.incrementViews(id);
        } catch (Exception e) {
            log.error("增加新闻浏览量失败", e);
            return Result.error("增加新闻浏览量失败");
        }
    }
} 